package com.gillion.grape.fros.ui.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import com.gillion.grape.core.utils.SU;

public class IOUtils {

	public static void closeQuietly(InputStream input) {
		try {
			if (input != null)
				input.close();
		} catch (IOException ioe) {
		}
	}

	public static void closeQuietly(OutputStream output) {
		try {
			if (output != null)
				output.close();
		} catch (IOException ioe) {
		}
	}

	public static int copy(InputStream input, OutputStream output)
			throws IOException {
		byte buffer[] = new byte[4096];
		int count = 0;
		for (int n = 0; -1 != (n = input.read(buffer));) {
			output.write(buffer, 0, n);
			count += n;
		}

		return count;
	}

	public static void copy(InputStream input, Writer output,String charset)
		throws IOException {
		InputStreamReader in = null;
		if(!SU.isEmpty(charset)){
			in = new InputStreamReader(input,charset);	
		}else{
			in = new InputStreamReader(input);
		}
		
		copy(((Reader) (in)), output);
	}
	
	public static void copy(InputStream input, Writer output)
			throws IOException {
		copy(input, output,null);
	}

	public static int copy(Reader input, Writer output) throws IOException {
		char buffer[] = new char[4096];
		int count = 0;
		for (int n = 0; -1 != (n = input.read(buffer));) {
			output.write(buffer, 0, n);
			count += n;
		}

		return count;
	}

	public static byte[] toByteArray(InputStream input) throws IOException {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		copy(input, output);
		return output.toByteArray();
	}

	public static String toString(InputStream input) throws IOException {
		return toString(input,null);
	}
	
	public static String toString(InputStream input,String charset) throws IOException {
		StringWriter sw = new StringWriter();
		if(SU.isEmpty(charset)){
			copy(input, sw);
		}else{
			copy(input, sw,charset);	
		}
		return sw.toString();
	}

	public static void write(String data, OutputStream output)
			throws IOException {
		if (data != null)
			output.write(data.getBytes());
	}

	public static void write(String data, OutputStream output, String encoding)
			throws IOException {
		if (data != null)
			if (encoding == null)
				write(data, output);
			else
				output.write(data.getBytes(encoding));
	}
	
	/**
	 * String 转 InputStream
	 * 
	 * @param str
	 * @return
	 * @author chich
	 * @since 20120206
	 */
	public static InputStream string2InputStream(String str) {
		ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
		return stream;
	}

	/**
	 * InputStream 转 String
	 * 
	 * @param is
	 * @return
	 * @author chich
	 * @since 20120206
	 */
	public static String inputStream2String(InputStream is) {
		BufferedReader in = new BufferedReader(new InputStreamReader(is));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		try {
			while ((line = in.readLine()) != null) {
				buffer.append(line);
				buffer.append("\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}
}
